<?php
//短信发送方法
function sendSms($mobile,$content){
	$account = C('smsname');
	$password = C('smspwd');
	$statusStr = array(
	"0" => "短信发送成功",
	"-1" => "参数不全",
	"-2" => "服务器空间不支持,请确认支持curl或者fsocket，联系您的空间商解决或者更换空间！",
	"30" => "密码错误",
	"40" => "账号不存在",
	"41" => "余额不足",
	"42" => "帐户已过期",
	"43" => "IP地址限制",
	"50" => "内容含有敏感词"
	);
	$smsapi = "http://www.smsbao.com/"; //短信网关
	$user = $account; //短信平台帐号
	$pass = md5($password); //短信平台密码
	$content = $content;//要发送的短信内容
	$phone = $mobile;
	$sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);
	$result = file_get_contents($sendurl);
	return $statusStr[$result];
    }
//安全码验证&&版本验证
function validate($data){
		$data = $data;
		$safecode = C('safecode');
		$apiversion = C('apiversion');
		if($safecode==$data['safecode']&&$apiversion==$data['apiversion']){
			$ReData['code'] = '200';
			$ReData['msg'] = '验证成功!';
			$ReData['data'] = $data;
		}else{
			$ReData['code'] = '-200';
			$ReData['msg'] = '验证失败!';
			$ReData['data'] = $data;
		}
		return $ReData;
}
/**
	 * 获取客户端IP地址
	 * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
	 * @param boolean $adv 是否进行高级模式获取（有可能被伪装）
	 * @return mixed
	 */
function client_ip($type = 0, $adv = true) {
    $type = $type ? 1 : 0;
    static $ip = NULL;
    if ($ip !== NULL) return $ip[$type];
    if ($adv) {
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
            $pos = array_search('unknown', $arr);
            if (false !== $pos)
                unset($arr[$pos]);
            $ip = trim($arr[0]);
        } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (isset($_SERVER['REMOTE_ADDR'])) {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
    } elseif (isset($_SERVER['REMOTE_ADDR'])) {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    // IP地址合法验证
    $long = sprintf("%u", ip2long($ip));
    $ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
    return $ip[$type];
}
//token生成
function user_token($data,$uid,$expire_time = 30){
	$user_token = md5($uid . $data . date('Y-m-d', time()) .  $expire_time);
	$data['uid'] = $uid;
	$data['user_token'] = $user_token;
	$data['expire_time'] = time() + ($expire_time*60);
	$map['uid'] = $uid;
	$Token = M('token');
	$toekninfo = $Token->where($map)->find();
	if($toekninfo){
		$res = $Token->save($data);
		if($res){
			return $user_token;
		}
	}else{
		$res = $Token->add($data);
		if($res){
			return $user_token;
		}
	}
}
//token校验
function check_token($token){
	$map['user_token'] = $token;
	$Token = M('token');
	$tokeninfo = $Token->where($map)->find();
	if($tokeninfo){
		return $tokeninfo['uid'];
	}else{
		return false;
	}
}
//资金记录
//function money_record($user_id,$type,$amount,$code,$status,$beizhu = '无备注'){
//	$data['create_time'] = time();
//	$data['user_id'] = $user_id;
//	$data['type'] = $type;
//	$data['amount'] = $amount;
//	$data['code'] = $code;
//	$data['status'] = $status;
//	$data['beizhu'] = $beizhu;
//	$MoneyRecord = M('money_record');
//	$money_record_res = $MoneyRecord ->add($data);
//	if($money_record_res){
//		return true;
//	}else{
//		return false;
//	}
//}
//站内信记录
//function message_record($user_id,$message,$send_user_id = 1){
//	$data['create_time'] = time();
//	$data['user_id'] = $user_id;
//	$data['message'] = $message;
//	$data['send_user_id'] = $send_user_id;
//	$messageRecord = M('message');
//	$message_res = $messageRecord ->add($data);
//	if($message_res){
//		return true;
//	}else{
//		return false;
//	}
//}
//上传图片
//function imgupload($base64img){
//	$base64_image = str_replace(' ', '+', $base64img);
//		//post的数据里面，加号会被替换为空格，需要重新替换回来，如果不是post的数据，则注释掉这一行
//	if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image, $result)){
//			//匹配成功
//			if($result[2] == 'jpeg'){
//				$image_name = uniqid().'.jpg';
//			}else{
//				$image_name = uniqid().'.'.$result[2];
//			}
//			$image_file = "./Public/Upload/images/{$image_name}";
//			//服务器文件存储路径
//			if (file_put_contents($image_file, base64_decode(str_replace($result[1], '', $base64_image)))){
//
//				 $imgPath = "/Public/Upload/images/".$image_name;
//				 $ReData['code'] ='200';//相应状态吗
//				 $ReData['msg'] = '上传成功';
//				 $ReData['data']['path'] = $imgPath;
//		}else{
//				$ReData['code'] ='-200';//相应状态吗
//				$ReData['msg'] = '上传失败';
//				$ReData['data']['base64img'] = $base64img;
//			}
//	}else{
//			$ReData['code'] ='-400';//相应状态吗
//			$ReData['msg'] = '传输数据格式错误';
//			$ReData['data']['base64img'] = $base64img;
//	}
//	return $ReData;
//}
//获取父级关系ID
function GetPidTree($t_id){
    $User = M('User')->find($t_id);
    static $UserList = array();
    $UserList[] = $User['id'];
    if($User['t_id'] != 0){
     return GetPidTree($User['t_id']); 
 	}
    return $UserList;

}

//数据excel文件(liuhaigang)
function getExcelData($file_name)
{
    set_time_limit(90);
    ini_set("memory_limit", "2014M");
    import("Org.Util.PHPExcel");
    import("Org.Util.PHPExcel.IOFactory");
    $objPHPExcel = PHPExcel_IOFactory::load($file_name);

    $sheet_count = $objPHPExcel->getSheetCount();
    $data = [];
    for ($s = 0; $s < $sheet_count; $s++)
    {
        $currentSheet = $objPHPExcel->getSheet($s);// 当前页
        $row_num = $currentSheet->getHighestRow();// 当前页行数
        $col_max = $currentSheet->getHighestColumn(); // 当前页最大列号

        // 循环从第二行开始，第一行往往是表头
        $tmp = [];
        for($i = 1; $i <= $row_num; $i++)
        {
            $cell_values = array();
            for($j = 'A'; $j <= $col_max; $j++) {
                $address = $j . $i; // 单元格坐标
                $cell_values[] = $currentSheet->getCell($address)->getFormattedValue();
            }

            $tmp[] = $cell_values;
        }
        $data[] = $tmp;
    }
    return $data;
}



?>